﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnisysMassiveImporter_v2.Entidades.Configuracion;
using UnisysMassiveImporter_v2.Datos.Archivos;

namespace UnisysMassiveImporter_v2.Entidades.Mensajes
{
    public class FileMessage:AMessage
    {

        public static int IO_Error = 3;
        //public bool errorFile { get; set; }

        public String file { get; set; }
        public FileMessage(String message,Exception exception,String file , int messageCode, String threadName)
        {
            this.message = message;
            this.exception = exception;
            this.file = file;
            this.messageType = IO_Error;
            this.messageCode = messageCode;
            this.threadName = threadName;
            //this.errorFile = false;
        }
        /*
        public FileMessage(String message, Exception exception, String file, int messageCode, Configuration appConfig, String threadName,bool errorFile)
        {
            this.message = message;
            this.exception = exception;
            this.file = file;
            this.messageType = IO_Error;
            this.messageCode = messageCode;
            this.threadName = threadName;
            this.appConfig = appConfig;
            this.errorFile=errorFile;
        }*/
        public FileMessage() {}

        public void sendMessage(String message, Exception exception, String file, int messageType, int messageCode, String threadName, ref Configuration appConfig)
        {
            this.message = message;
            this.exception = exception;
            this.file = file;
            this.messageType = messageType;
            this.messageCode = messageCode;
            this.threadName = threadName;
            this.sendMessage(ref appConfig);
        }

        public override String toString()
        {
            String tempMsg = "";
            if (this.exception == null)
                tempMsg = DateTime.Now.ToString("yyyy'-'MM'-'dd HH':'mm':'ss':'fff") + " - " + Logger.ErrorMessage(this.messageType) + " - " + this.message + "\r\n";
            else
                tempMsg = DateTime.Now.ToString("yyyy'-'MM'-'dd HH':'mm':'ss':'fff") + " - " + Logger.ErrorMessage(this.messageType) + " - " + this.message + "-" + this.exception.Message+ ". Archivo con errores: "+this.file + "\r\n";
            return tempMsg;
        }

        public override void sendMessage(ref Configuration appConfig)
        {
            if (Logger.ifWrite(this.messageType))
            {
                if (Logger.ifFileWrite())
                {
                    try
                    {
                        Logger.fileWrite(this, ref appConfig);
                    }
                    catch { }
                }

                try
                {
                    using (var proxy = new IServicioError.ServicioErrorClient("NetTcpBinding_IServicioError"))
                    {
                        var error = new IServicioError.ErrorGeneral();
                        error.Fecha = DateTime.Now;
                        error.FechaReal = DateTime.Now;
                        error.CodError = this.messageCode;
                        error.CodSistema = appConfig.system;
                        error.DetalleError = this.toString();
                        proxy.Agregar(error);
                    }
                }
                catch (Exception ex)
                {
                    this.message = "Error al enviar al Web Service de Error el siguiente mensaje(" + ex.Message + "): " + this.message;
                    try{
                        Logger.fileWrite(this, ref appConfig);
                    }
                    catch (Exception e)
                    {
                        Logger.LogThis4Windows("El sistema de bitacoras esta caido, imposible continuar con la aplicacion[mensaje: " + this.message + "]", "Unisys Masive Importer", "Unisys Masive Service");
                        System.Environment.Exit(0);
                    }
                }

               
            }
        }



    }
}
